package org.cycuchy.hp.components.book.map {
	
	import com.rubenswieringa.book.BookEvent;
	
	import flash.events.TextEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.controls.Text;
	import mx.events.FlexEvent;
	
	import org.cycuchy.hp.components.book.NumeredPage;

	use namespace com.rubenswieringa.book.limited;
	
	public class MapPageControl_control extends NumeredPage {
		
		[Bindable]
		public var mapPage:MapPage;
		
		public var txtHome:Text;
		public var txtJob:Text;
		
		private static var DG:String = "dg";
		private static var DKC1:String = "dkc1";
		private static var DKC2:String = "dkc2";
		private static var PC1:String = "pc1";
		private static var PC2:String = "pc2";
		
		private var doTimer:Boolean;
		
		public function MapPageControl_control() {
			super();
			addEventListener(BookEvent.CURRENTPAGE_CHANGED, currentPageChanged);
			addEventListener(FlexEvent.CREATION_COMPLETE, creationComplete);  
			addEventListener(TextEvent.LINK, link);
		}
		
		private function currentPageChanged(event:Event):void {
//			trace("UMapPageControl::currentPageChanged");
			var b:Boolean = book.currentPage == this.index;
			selectable(b); 
			if (b) {
				doTimer = true;
				timer(null);
			} else {
				doTimer = false;
			}
		}
		
		private function creationComplete(event:FlexEvent):void {
			txtHome.htmlText = getTextHome();
			txtJob.htmlText = getTextJob();
		}
		
		private function timer(event:TimerEvent):void {
//			trace("timer");
			txtHome.htmlText = getTextHome();
			txtJob.htmlText = getTextJob();
			if (doTimer == true) {
				var t:Timer = new Timer(5000, 1);
				t.addEventListener(TimerEvent.TIMER, timer);
				t.start();
			}
		}
		
		private function selectable(value:Boolean):void{
			txtHome.selectable = value;
			txtJob.selectable = value;
		}
		
		private static var hstrings:Array = ["my ", "home", " is", " my", " castle "];
		private static var hdstrings:Array = [DG, DKC1, DKC2];
				
		private function getTextHome():String {
			var s:String = "";
			var r:Number;
			var r10:Boolean, r20:Boolean, r30:Boolean, home:Boolean, added:Boolean;
			for each (var dstring:String in hdstrings) {
				added = false;
				for (var i:uint=0; i<5; i++) {
					for each (var string:String in hstrings) {
						r = uint(Math.random() * 100);
						r10 = r%10 > 5;
						r20 = r%20 < 10;
						r30 = r%30 > 15;
						home = string == "home" && ((i+1)*r)%3 == 0;
						
//						trace(r10, r20, r30);
						if (r10) {
							s += "<b>";							
						}
						if (r30) {
							s += "<i>";							
						}
						if (r20) {
							s += "<font size='+" + r%20 + "'>";							
						}
						
						if (home && !added) {
						 	s += "<font color='#434343'><a href='event:" + dstring + "'>";
						}
						
						s += string;
						
						if (home && !added) {
							s += "</a></font>";
							added = true;
						}
						
						if (r20) {
							s += "</font>";
						}						
						if (r30) {
							s += "</i>";							
						}
						if (r10) {
							s += "</b>";
						}
					}
				}
			}
//			trace(s);
			return s;
		}
		
		private static var jstrings:Array = ["no ", "job", " -", " no", " problem "];
		private static var jpstrings:Array = [PC1, PC2];
		
		private function getTextJob():String {
			var s:String = "";
			var r:Number;
			var r10:Boolean, r20:Boolean, r30:Boolean, home:Boolean, added:Boolean;
			for each (var dstring:String in jpstrings) {
				added = false;
				for (var i:uint=0; i<5; i++) {
					for each (var string:String in jstrings) {
						r = uint(Math.random() * 100);
						r10 = r%10 > 5;
						r20 = r%20 < 10;
						r30 = r%30 > 15;
						home = string == "job" && ((i+1)*r)%2 == 0;
						
//						trace(r10, r20, r30);
						if (r10) {
							s += "<b>";							
						}
						if (r30) {
							s += "<i>";							
						}
						if (r20) {
							s += "<font size='+" + r%20 + "'>";							
						}
						
						if (home && !added) {
						 	s += "<font color='#434343'><a href='event:" + dstring + "'>";
						}
						
						s += string;
						
						if (home && !added) {
							s += "</a></font>";
							added = true;
						}
						
						if (r20) {
							s += "</font>";
						}						
						if (r30) {
							s += "</i>";							
						}
						if (r10) {
							s += "</b>";
						}
					}
				}
			}
//			trace(s);
			return s;
		}
		
		private function link(event:TextEvent):void {
//			trace("UMapPageControl::onLinkClick", event);
			switch (event.text) {
				case DG:
					mapPage.domGdansk();
					break;
				case DKC1:
					mapPage.domKetrzynCycek1();
					break;
				case DKC2:
					mapPage.domKetrzynCycek2();
					break;
				case PC1:
					mapPage.pracaCycek1();
					break;
				case PC2:
					mapPage.pracaCycek2();
					break;
			}
		}
		
	}
	
}